/**
 * Created by Jack Xue on 20101202
 */
package org.code.jack.jweberp.web;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.code.jack.jweberp.domain.SystemDictionary;
import org.code.jack.jweberp.service.ISystemDictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

@Controller("/systemDictionary")
public class SystemDictionaryAction extends BaseAction {
	@Resource
	private ISystemDictionaryService systemDictionaryService;

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
//		SystemDictionaryQuery qo = (SystemDictionaryQuery) webForm.form2Obj(SystemDictionaryQuery.class);
//		IPageList pageList = systemDictionaryService.getByQueryObject(qo);
//		CommUtil.IPageListToWebView(pageList, request);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("edit");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			SystemDictionary obj = systemDictionaryService.getEntity(new Long(id));
			request.setAttribute("obj", obj);
		}
		return mapping.findForward("edit");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			SystemDictionary obj = systemDictionaryService.getEntity(new Long(id));
			if (obj != null) {
				webForm.form2Obj(obj, true);
				systemDictionaryService.updateEntity(obj);
				request.setAttribute("msg", "更新成功");
			}
		}
		request.setAttribute("newurl", veerUrl);
		return this.list(mapping, form, request, response);
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		SystemDictionary sd = (SystemDictionary) webForm.form2Obj(SystemDictionary.class, true);
		systemDictionaryService.createEntity(sd);
		request.setAttribute("newurl", veerUrl);
		request.setAttribute("msg", sd.getId() != null ? "添加成功" : "添加失败");
		return this.list(mapping, form, request, response);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id").trim();
		if (StringUtils.hasText(id)) {
			Boolean b = systemDictionaryService.deleteEntity(new Long(id));
			request.setAttribute("msg", b ? "删除成功" : "删除失败");
		}
		return this.list(mapping, form, request, response);
	}

	@Override
	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return index(mapping, form, request, response);
	}

	public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		return list(mapping, form, request, response);
	}
}
